package nccloud.web.pu.buyingreq.action;

import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillSupplierVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.scmpub.res.billtype.POBillType;
import nc.vo.scmpub.util.AppInfoContext;
import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.core.util.GridCompareUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCardOperator;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.pubitf.scmpub.commit.service.IBatchRunScriptService;
import nccloud.web.pu.buyingreq.entity.QueryInfoBuyingreq;
import nccloud.web.pu.buyingreq.util.BuyingReqScaleUtil;

public class BuyingreqInfoNewSaveAction implements ICommonAction {
	public Object doAction(IRequest request) {
		ExtBillCard card = null;
		ExtBillCard returnCard = null;
		String read = request.read();
		IJson json = JsonFactory.create();

		QueryInfoBuyingreq query = (QueryInfoBuyingreq) json.fromJson(read, QueryInfoBuyingreq.class);
		String type = query.getType();

		ExtBillCardOperator extOperator = new ExtBillCardOperator();
		BillCardOperator operator = new BillCardOperator(query.getTempletid(), query.getPageid());
		PraybillVO[] vos = null;
		try {
			PraybillVO vo = (PraybillVO) extOperator.toBill(request);
			BillCard oldCard = operator.toCardBySpecialBodyArea(vo, "card_body");
			afterProcess(oldCard);
//			//校验请购单是否输入供应商行
//			if (!checkSupplier(vo)) {
//				ExceptionUtils.wrapException(new BusinessException("请填写供应商信息!"));
//			}
			vo.getParent().setStatus(2);
			vos = new PraybillVO[] { vo };
			CloudPFlowContext context = new CloudPFlowContext();
			context.setBillType(POBillType.PrayBill.getCode());
			context.setBillVos(vos);
			context.setActionName("SAVEBASE");

			AppInfoContext.setBtnCode("Save");

			SCMScriptResultDTO obj = ((IBatchRunScriptService) ServiceLocator.find(IBatchRunScriptService.class))
					.runBacth(context, PraybillVO.class);

			AbstractBill[] sucessVOs = obj.getSucessVOs();
			if (sucessVOs == null || sucessVOs.length < 1) {
				return null;
			}
			card = extOperator.toCard(sucessVOs[0]);
			BillCard billCard = operator.toCardBySpecialBodyArea(sucessVOs[0], "card_body");
			afterProcess(billCard);

			if (type != null) {
				return card;
			}
			returnCard = GridCompareUtils.compareExtBillCardGrid(extOperator.getOriginBillcard(), card);
		} catch (Exception e) {
			ExceptionUtils.wrapException(e);
		}
		return returnCard;
	}

	public void afterProcess(BillCard card) {
		BuyingReqScaleUtil buyUtil = new BuyingReqScaleUtil();
		buyUtil.processBillCard(card, false);
	}
	
	public boolean checkSupplier(PraybillVO vo) {
		PraybillItemVO[] itemVOs = (PraybillItemVO[]) vo.getChildren(PraybillItemVO.class);
		for (PraybillItemVO itemVO : itemVOs) {
			if (itemVO.getCsourceid() != null && !"".equals(itemVO.getCsourceid())) {
				return true;
			}
		}
		
		PraybillSupplierVO[] supplierVOs = (PraybillSupplierVO[]) vo.getChildren(PraybillSupplierVO.class);
		if (supplierVOs == null || supplierVOs.length == 0) {
			return false;
		}
		for (PraybillSupplierVO supplierVO : supplierVOs) {
			if (supplierVO == null || supplierVO.getPk_supplier_v() == null || "".equals(supplierVO.getPk_supplier_v())) {
				return false;
			}
		}
		return true;
	};
}
